home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 5972 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  80 lines

  1. Path: netnews.jhuapl.edu!usenet
  2. From: "Daniel J. Levine" <einstein@universe.jhuapl.edu>
  3. Newsgroups: comp.lang.c++
  4. Subject: [Q] Heirarchies and Comparison Operators...
  5. Date: Wed, 07 Feb 1996 13:46:54 -0500
  6. Organization: Johns Hopkins University Applied Physics Laboratory
  7. Message-ID: <3118F39E.6916@universe.jhuapl.edu>
  8. NNTP-Posting-Host: f2ahp2.jhuapl.edu
  9. Mime-Version: 1.0
  10. Content-Type: text/plain; charset=us-ascii
  11. Content-Transfer-Encoding: 7bit
  12. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/735)
  13.  
  14. I've been trying (to no avail) to think of a good way to
  15. solve the following problem:
  16.  
  17. If you have a class heirarchy like this:
  18.  
  19.  
  20. class point
  21. {
  22. }
  23.  
  24. class point1D : public point
  25. {
  26. private:
  27. int x;
  28. }
  29.  
  30. class point2D : public point1D
  31. {
  32. private:
  33. int y;
  34. }
  35.  
  36. class point3D : public point2D
  37. {
  38. private:
  39. int z;
  40. }
  41.  
  42. I would now like to add operator==() and operator!=() to this heirarchy
  43. with the following meaning criteria for being equal:
  44.  
  45. 1. You should be able to compare any of the 3 types (and pointers to
  46. them)
  47.    and get the same results when the type is on the left or the right
  48. hand
  49.    side of the operator.
  50.  
  51. 2. I don't have a compiler with RTTI, but I'd like to make comparisons
  52.    between different types to be not equal.  I didn't want to use a
  53.    magic constant for each type if it was avoidable.
  54.  
  55. 3. If all the dimensions have the exact same values, they are equal.
  56.  
  57. I was playing with a solution which looked like this:
  58.  
  59. virtual int point::operator==(const point& rhs) = 0;
  60.  
  61. virtual int point1D::operator==(const point& rhs)
  62. {
  63.    // what would I put in here?
  64. }
  65.  
  66. virtual int point2D::operator==(const point& rhs)
  67. {
  68.    // and here?
  69. }
  70.  
  71. virtual int point3D::operator==(const point& rhs)
  72. {
  73.    // and here?
  74. }
  75.  
  76.  
  77. Any help would be greatly appreciated!
  78.  
  79. -Dan
  80.